const fs = require('fs'),
          path = require('path'),
              cmd = process.argv[2],
                  file = process.argv[3];

if (process.argv.length === 3 && cmd === '-list') {
  try {
            let files = fs.readdirSync('./sysFile')
                      let data = []
                            
                      for (let i=0; i<files.length; i++) {
                                    let file = path.join('./sysFile', files[i])
                                            
                                      if (fs.statSync(file).isFile()) {
                                              
                                                        let fileName = files[i]
                                                                          let fileSize = fs.statSync(file).size
                                                                                          let createTime = fs.statSync(file).birthtime
                                                                                                
                                                                                          let fileObj = {
                                                                                                                fileName: fileName,
                                                                                                                                    fileSize: fileSize,
                                                                                                                                                        createTime: createTime
                                                                                                                                                                          
                                                                                          }
                                                              
                                                                        data.push(fileObj)
                                                                                      
                                      } else {
                                                        let dirName = files[i]
                                                                          let dirSize = fs.statSync(file).size
                                                                                          let createTime = fs.statSync(file).birthtime
                                                                                                
                                                                                          let dirObj = {
                                                                                                                dirName: dirName,
                                                                                                                                    dirSize: dirSize,
                                                                                                                                                        createTime: createTime
                                                                                                                                                                          
                                                                                          }
                                                              
                                                                        data.push(dirObj)
                                                                                      
                                      }
                                            
                      }
                  
            for (let i=0; i<data.length-1; i++) {
              for (let j=0; j<data.length-1-i; j++) {
                if (data[j].createTime > data[j+1].createTime) {
                                      let tmp = data[j]
                                                            data[j] = data[j+1]
                                                                                data[j+1] = tmp
                                                                                                
                }
                            
              }
                      
            }
                  
                    console.log(JSON.stringify(data, null, 4))
                            
                            
  } catch (e) {
            console.error(e.message)
                      process.exit(1)
                            
  }

}

if (process.argv.length === 4 &&  cmd === '-show') {
  if (!fs.existsSync(file)) {
            console.error(`${file} not exist!`)
                      process.exit(2)
                          
  }
        
  if (!fs.statSync(file).isFile()) {
            console.error(`${file} is not file!`)
                      process.exit(3)
                          
  }
        
  try {
            const content = fs.readFileSync(file).toString('utf8')
                      console.log(content)
                          
  } catch (e) {
            console.error(e.message)
                      process.exit(4)
                          
  }

}

if (process.argv.length === 4 && cmd === '-del') {
  if (typeof file === 'undefined') {
            console.error('请指定要删除的文件名或者目录名！')
                      process.exit(5)
                          
  }
        
  if (!fs.existsSync(file)) {
            console.error(`${file} not exist!`)
                      process.exit(6)
                          
  }
        
  if (fs.statSync(file).isFile()) {
    try {
                  fs.unlinkSync(file)
                                console.log(`${file}删除成功`)
                                        
    } catch (e) {
                  console.error(e.message)
                                process.exit(7)
                                        
    }
        
  }
        
  if (fs.statSync(file).isDirectory()) {
            deleteDir(file)
                  
  }
        
  function deleteDir(folder) {
            let files = fs.readdirSync(folder)
                    
              for (let i=0; i<files.length; i++) {
                            let file = path.join(folder, files[i])
                                    
                              if (fs.statSync(file).isFile()) {
                                                fs.unlinkSync(file)
                                                                  console.log(`${file}删除成功`)
                                                                                  continue
                                                                                              
                              }
                                  
                            if (fs.statSync(file).isDirectory()) {
                                              deleteDir(file)
                                                            
                            }
                                    
              }  

            try {
                          fs.rmdirSync(folder)
                                        console.log(`${folder}删除成功`)
                                                
            } catch (e) {
                          console.error(e.message)
                                        process.exit(8)
                                                
            }
                
  }

}
  


